﻿using Core.Factories;
using Core.Context;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

/*http://jamesheppinstall.wordpress.com/2012/07/01/windows-communication-foundation-resolving-mvc-client-wcf-channelfactory-dependencies-with-unity/*/
namespace ProxyServices
{    
    public class ProxyServiceFactory<T> : IProxyServiceFactory<T>
    {
        private readonly IFrontConfig settings;
        public ProxyServiceFactory(IFrontConfig settings)
        {
            this.settings = settings;
        }

        public T GetInstance()
        {
            string interfaceName = typeof(T).Name;
            string type = interfaceName.Substring(1).Replace("Service", "");

            string url = string.Format("{0}/{1}.svc", settings.WcfUrl, type);
            BasicHttpBinding myBinding = new BasicHttpBinding();
            EndpointAddress myEndpoint = new EndpointAddress(url);

            ChannelFactory<T> myChannelFactory = new ChannelFactory<T>(myBinding, myEndpoint);
            return myChannelFactory.CreateChannel();
        }
    }
}
